<?php
class ArticleController extends CommonController{
	public function Index(){
		$lang_item	= $this->cache->read('lang_item');
		$category	= $this->cache->read('category');
		$count = $this->db->table("@_article")->count();
		$Page       = new Page($count, 20);
		$list	= $this->db->limit("$Page->firstRow,$Page->listRow")->order("id DESC")->findAll();
		foreach ($list as $key => $v) {
			$list[$key]['lang']			= $lang_item[$v['lang_id']];
			$list[$key]['category']		= $category[$v['category_id']];
		}
		$pageHtml = $Page->show();
		include $this->display('article.html');
	}

	public function Add(){
		$isPost		= isset($_GET['isPost']) ? intval($_GET['isPost']) : 0;
		if (subMit('data')) {
			$data            = getGpc('data', 'string', 'P');
			$editorValue     = getGpc('editorValue', 'string', 'P');
			$data['lang_id'] = intval($data['lang_id']);
			if (empty($data['topic'])) {
				$this->msg('请填写标题...');
			}
			if (empty($data['category_id'])) {
				$this->msg('请选择类别...');
			}
			if (empty($data['lang_id'])) {
				$this->msg('请选择语言...');
			}
			if (empty($editorValue)) {
				$this->msg('请填写内容...');
			}
			if ($editorValue == '<p><br /></p>') {
				$this->msg('请填写内容...');
			}
			$data['dateline']	= time();
			$data['content']	= $editorValue;
			$this->db->table("@_article")->add($data);
			$result['status']	= 1;
			$result['msg']		= '保存成功';
			echo json_encode($result);
			exit();
		}
		$lang_item	= $this->cache->read('lang_item');
		$category	= $this->cache->read('category');
		include $this->display('article.html');
	}

	public function Edit(){
		$id 		= getGpc('id', 'integer', 'G');
		if (empty($id)) {
			$this->msg('参数错误。');
		}
		if ( subMit('data') ){
			$data            = getGpc('data', 'string', 'P');
			$editorValue     = getGpc('editorValue', 'string', 'P');
			$data['lang_id'] = intval($data['lang_id']);
			if (empty($data['topic'])) {
				$this->msg('请填写标题...');
			}
			if (empty($data['category_id'])) {
				$this->msg('请选择类别...');
			}
			if (empty($data['lang_id'])) {
				$this->msg('请选择语言...');
			}
			if (empty($editorValue)) {
				$this->msg('请填写内容...');
			}
			if ($editorValue == '<p><br /></p>') {
				$this->msg('请填写内容...');
			}
			$data['content']	= $editorValue;
			$this->db->table("@_article")->where("id=$id")->update($data);
			$result['status']	= 1;
			$result['msg']		= '修改成功';
			echo json_encode($result);
			exit();
		}
		$vo = $this->db->table("@_article")->where("id=$id")->find();
		if (empty($vo)) {
			$this->msg('不存在此文章');
		}
		$lang_item	= $this->cache->read('lang_item');
		$category	= $this->cache->read('category');
		include $this->display('article.html');
	}
	// 文章类别
	public function Category(){
		$lang_item	= $this->cache->read('lang_item');
		$list = $this->db->table("@_article_category")->order("lang_id ASC,id ASC")->findAll();
		foreach ($list as $key => $v) {
			$list[$key]['lang_str']		= $lang_item[$v['lang_id']];
		}
		include $this->display('article.html');
	}

	public function Addcategory(){
		if ( subMit('data') ){
			$data				= getGpc('data', 'string', 'P');
			$data['lang_id'] 	= intval($data['lang_id']);
			if (empty($data['name'])) {
				$this->msg('请填写名称。');
			}
			if (empty($data['lang_id'])) {
				$this->msg('请选择语言');
			}
			$this->db->table("@_article_category")->add($data);
			$this->_categoryCache();
			$result['status']	= 1;
			$result['msg']		= '添加成功';
			echo json_encode($result);
			exit();
		}
		$lang_item	= $this->cache->read('lang_item');
		ob_start();
		include $this->display('article.html');
		$content = ob_get_contents() ;
		ob_end_clean() ;
		$result['status']	= 1;
		$result['content']	= $content;
		echo json_encode($result);
	}

	public function Editcategory(){
		$id 		= getGpc('id', 'integer', 'G');
		if (empty($id)) {
			$this->msg('参数错误。');
		}
		if ( isset($_POST) && $_POST ){
			$data				= getGpc('data', 'string', 'P');
			$data['lang_id'] 	= intval($data['lang_id']);
			if (empty($data['name'])) {
				$this->msg('请填写名称。');
			}
			if (empty($data['lang_id'])) {
				$this->msg('请选择语言');
			}
			$this->db->table("@_article_category")->where("id=$id")->update($data);
			$this->_categoryCache();
			$result['status']	= 1;
			$result['msg']		= '修改成功';
			echo json_encode($result);
			exit();
		}
		$vo = $this->db->table("@_article_category")->where("id=$id")->find();
		if (empty($vo)) {
			$this->msg('不存在此类别');
		}
		$lang_item	= $this->cache->read('lang_item');
		ob_start();
		include $this->display('article.html');
		$content = ob_get_contents() ;
		ob_end_clean() ;
		$result['status']	= 1;
		$result['content']	= $content;
		echo json_encode($result);
	}

	private function _categoryCache(){
		$list     = $this->db->table("@_article_category")->order("id ASC")->findAll();
		$category = array();
		foreach ($list as $key => $v) {
			$category[$v['id']]	= $v['name'];
		}
		$this->cache->write($category, 'category');
	}
}
?>